﻿using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using ZGRemote.Common.Extensions;

namespace ZGRemote.Server.UI.Resources
{
    public class ResourceService : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;

        private static readonly ResourceService _current = new ResourceService();
        public static ResourceService Current => _current;

        private readonly static System.Resources.ResourceManager resourceManager  = Core.Properties.Resources.ResourceManager;

        public string this[string name]
        {
            get
            {
                return resourceManager.GetString(name, Core.Properties.Resources.Culture) ?? "";
            }
        }

        public void ChangedCulture(LanguageEnum language)
        {
            Core.Properties.Resources.Culture = CultureInfo.GetCultureInfo(language.GetDescription());
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("item[]"));
        }

        public LanguageEnum GetCurrentLanguage()
        {
            var currentLanguage = Core.Properties.Resources.Culture?.ToString() ?? CultureInfo.CurrentCulture.ToString();

            foreach (LanguageEnum item in Enum.GetValues(typeof(LanguageEnum)))
            {
                if(item.GetDescription().ToUpper().Equals(currentLanguage.ToUpper()))
                {
                    return item;
                }
            }
            return LanguageEnum.English;
        }

    }

    public enum LanguageEnum
    {
        [Description("zh-CN")]
        简体中文,
        [Description("")]
        English
    }
}
